
package com.sun.showcase.biz.testcase.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.testcase.dao.TaskCaseDao;
import com.sun.showcase.biz.testcase.service.CaseLibraryService;
import com.sun.showcase.biz.testcase.service.TaskCaseService;
import com.sun.showcase.biz.testcase.service.TestTaskService;
import com.sun.showcase.client.testcase.domain.CaseLibrary;
import com.sun.showcase.client.testcase.domain.TaskCase;
import com.sun.showcase.client.testcase.domain.TestTask;
import com.sun.showcase.client.testcase.query.TaskCaseQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("taskCaseService")
@Transactional
public class TaskCaseServiceImpl implements TaskCaseService{
	@Autowired
	private TaskCaseDao taskCaseDao;
	@Autowired
	private CaseLibraryService caseLibraryService;
	@Autowired
	private TestTaskService testTaskService;
	
	public void setTaskCaseDao(TaskCaseDao dao) {
		this.taskCaseDao = dao;
	}

	public DataGrid datagrid(TaskCaseQuery taskCaseQuery) {
		DataGrid j = new DataGrid();
		Pager<TaskCase> pager  = find(taskCaseQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<TaskCaseQuery> getQuerysFromEntitys(List<TaskCase> taskCases) {
		List<TaskCaseQuery> taskCaseQuerys = new ArrayList<TaskCaseQuery>();
		if (taskCases != null && taskCases.size() > 0) {
			for (TaskCase tb : taskCases) {
				TaskCaseQuery b = new TaskCaseQuery();
				BeanUtils.copyProperties(tb, b);
				taskCaseQuerys.add(b);
			}
		}
		return taskCaseQuerys;
	}

	private Pager<TaskCase> find(TaskCaseQuery taskCaseQuery) {
		Page<TaskCase> page = PageHelper.startPage(taskCaseQuery.getPage().intValue(), taskCaseQuery.getRows().intValue());
		List<TaskCase> list = taskCaseDao.findList(taskCaseQuery);
		Pager<TaskCase> pager = new Pager<TaskCase>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	@Override
	public TaskCase getByTaskAndCaseId(TaskCaseQuery taskCaseQuery) {
		return taskCaseDao.getByTaskAndCaseId(taskCaseQuery);
	}

	public ExecuteResult<TaskCase> add(TaskCaseQuery taskCaseQuery) {
		ExecuteResult<TaskCase> res = new ExecuteResult<TaskCase>();
		if(this.getByTaskAndCaseId(taskCaseQuery) != null){
			return res;
		}
		TaskCase t = new TaskCase();
		TestTask task = testTaskService.get(taskCaseQuery.getTaskId());
		taskCaseQuery.setTaskName(task.getName());
		BeanUtils.copyProperties(taskCaseQuery, t);
		if(taskCaseQuery.getCaseId() != null){
			CaseLibrary cl = caseLibraryService.get(taskCaseQuery.getCaseId());
			if(cl == null){
				res.addErrorMessage("无效用例");
				return res;
			}
			BeanUtils.copyProperties(cl, t);
			t.setCaseId(cl.getId());
		}else{
			res.addErrorMessage("无效用例");
			return res;
		}
		t.setStatus(StringConstant.ZERO);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
		taskCaseDao.save(t);
		taskCaseQuery.setId(t.getId());
		res.setResult(t);
		return res;
		
	}

	@Override
	public ExecuteResult<TaskCase> addBatch(Long taskId,Long taskGroupId, Long[] caseIds) {
		ExecuteResult<TaskCase> res = new ExecuteResult<TaskCase>();
		TaskCaseQuery taskCaseQuery = new TaskCaseQuery();
		taskCaseQuery.setTaskId(taskId);
		for(Long caseId : caseIds){
			taskCaseQuery.setCaseId(caseId);
			if(taskGroupId==null) {
				taskCaseQuery.setTaskGroupId(0L);
			}else {
				taskCaseQuery.setTaskGroupId(taskGroupId);
			}
			this.add(taskCaseQuery);
		}
		return res;
	}

	public ExecuteResult<TaskCase> update(TaskCaseQuery taskCaseQuery) {
		ExecuteResult<TaskCase> res = new ExecuteResult<TaskCase>();
		TaskCase t = taskCaseDao.getById(taskCaseQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(taskCaseQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	taskCaseDao.update(t);
		}
	    return res;
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(TaskCaseQuery taskCaseQuery) {
	    taskCaseDao.updatePart(taskCaseQuery);
	}

	public void delete(java.lang.Long[] ids) {
		taskCaseDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Long[] ids) {
		taskCaseDao.deletePt(ids);
	}

	public TaskCase get(TaskCaseQuery taskCaseQuery) {
		return taskCaseDao.getById(new Long(taskCaseQuery.getId()));
	}

	public TaskCase get(Long id) {
		return taskCaseDao.getById(id);
	}

	
	public List<TaskCaseQuery> listAll(TaskCaseQuery taskCaseQuery) {
	    List<TaskCase> list = taskCaseDao.findList(taskCaseQuery);
		List<TaskCaseQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void updateStatusBatch(TaskCaseQuery taskCaseQuery) {
		taskCaseDao.updateStatusBatch(taskCaseQuery);
	}

	@Override
	public int getStatusCount(TaskCaseQuery taskCaseQuery) {
		return taskCaseDao.getStatusCount(taskCaseQuery);
	}

	@Override
	public void updateGroupId(TaskCaseQuery taskCaseQuery) {
		if(taskCaseQuery.getTaskGroupId() == null || taskCaseQuery.getMoveToTaskId() == null) {
			return;
		}
		if(taskCaseQuery.getIds() != null && taskCaseQuery.getIds().length > 0) {
			List<Long> ids = new ArrayList<Long>();
			for(Long id : taskCaseQuery.getIds()) {
				TaskCase tc = this.get(id);
				TaskCaseQuery tmp2 = new TaskCaseQuery();
				tmp2.setTaskId(taskCaseQuery.getMoveToTaskId());
				tmp2.setCaseId(tc.getCaseId());
				tmp2.setTaskGroupId(taskCaseQuery.getTaskGroupId());
				if(this.getByTaskAndCaseId(tmp2) == null){
					ids.add(id);
				}
			}
			if(ids.size()>0) {
				TaskCaseQuery tmp = new TaskCaseQuery();
				tmp.getSearchMap().put("ids", ids);
				tmp.setTaskGroupId(taskCaseQuery.getTaskGroupId());
				tmp.setTaskId(taskCaseQuery.getMoveToTaskId());
				this.updatePart(tmp);
			}
		}else if(taskCaseQuery.getOriginalGroupId() != null) {
			TaskCaseQuery tmp = new TaskCaseQuery();
			tmp.setTaskGroupId(taskCaseQuery.getOriginalGroupId());
			List<TaskCaseQuery> list = this.listAll(tmp);
			TaskCaseQuery tmp2 = new TaskCaseQuery();
			for(TaskCaseQuery tc : list) {
				tmp2.setTaskId(taskCaseQuery.getMoveToTaskId());
				tmp2.setCaseId(tc.getCaseId());
				tmp2.setTaskGroupId(taskCaseQuery.getTaskGroupId());
				if(this.getByTaskAndCaseId(tmp2) == null){
					tmp.getSearchMap().put("id", tc.getId());
					tmp.setTaskGroupId(taskCaseQuery.getTaskGroupId());
					tmp.setTaskId(taskCaseQuery.getMoveToTaskId());
					this.updatePart(tmp);
				}
			}
		}
	}
	
	
}
